import 'package:dio/dio.dart' as ddd;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:restart_app/restart_app.dart';
import 'package:universal_html/html.dart';

import '../services/storage_service.dart';
import '../utils/consts.dart';
import '../utils/logger.dart';
import 'special_code_pop.dart';

//// 处理特殊业务码
abstract class HttpSpecialCodeHandler {
  static void doAction(ddd.Response<dynamic> response) {
    var code = 200;
    final resData = response.data;
    if (resData is Map) {
      code = resData["code"];
    }
    logger.d("special code >>>>> $code");
    //其他的情况都重新登陆 301 302 1001 1002 1003
    if (SmartDialog.checkExist()) {
      return;
    }
    SmartDialog.showLoading(
      msg: '',
      animationBuilder: (controller, child, animationParam) {
        return SpecialCodePop(
          desc: '请点击确定重新初始化app',
          tap: () {
            final storageService = Get.find<StorageService>();
            storageService.deleteBox(
              Consts.generalBoxName,
            );
            if (kIsWeb) {
              window.location.assign(window.location.origin);
            } else {
              SmartDialog.dismiss();
              Restart.restartApp();
            }
          },
        );
      },
      alignment: Alignment.center,
    );
  }
}
